import java.util.*;
import java.io.*;
public class test {
    public static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    // 蓝桥杯 封闭图形个数(⭐⭐⭐)
    public static int[] nums = {1,0,0,0,1,0,1,0,2,1};
    // 0 1 2 3 4 5 6 7 8 9
    public static int INF = 1000000000;
    public static void main(String[] args) throws IOException{
        int n = in.nextInt();
        int[] arr = new int[n];
        long[] price = new long[n];
        //为了避免处理数字本身大小.
        //将一个封闭图形个数.记录为1e9(大于a的最大值,最后将a本身加入,排序时则可忽略)
        for(int i = 0;i < n;i++) {
            arr[i] = in.nextInt();
            price[i] = 1l * findNum(arr[i]) * INF + arr[i] - 1;
        }
        Arrays.sort(price);
        //处理如何还原数组?
        //存储price时,所有数先-1,将a的范围变成[0~1e9-1]
        //还原时对所有数字对1e9取余,然后+1即可~
        for(int i = 0;i < n;i++) {
            System.out.print(price[i] % INF + 1 + " ");
        }
    }
    public static int findNum(int num) {
        int sum = 0;
        while(num != 0) {
            //如果为封闭图形,则加封闭图形数量,否则加0
            int n = num % 10;
            if(n == 0 || n == 4 || n == 6 || n == 9) {
                sum++;
            }else if(n == 8) {
                sum += 2;
            }
            num /= 10;
        }
        return sum;
    }
    // 蓝桥杯 错误票据(⭐⭐⭐)
    public static HashSet<Integer> set;
    public static int INF = 10005;
    public static void main1(String[] args) throws IOException{
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        set = new HashSet<Integer>();
        int index1 = 0;
        int index2 = 0;
        int[] arr = new int[10005];
        Arrays.fill(arr, INF);
        int index = 0;
        while(n-- > 0) {
            while(in.hasNextInt()) {
                int num = in.nextInt();
                arr[index++] = num;
            }
        }
        Arrays.sort(arr);
        for(int i = 0;i < 9999;i++) {
            //代表检索结束
            if(arr[i + 1] == INF) break;
            //大于1则代表发生了断号
            if(arr[i + 1] - arr[i] > 1) index1 = arr[i] + 1;
            if(!set.contains(arr[i])) set.add(arr[i]);
            else {
                index2 = arr[i];
            }
        }
        System.out.println(index1 == 0 ? arr[0] : index1 + " " + index2);
    }
    // 蓝桥杯 三国游戏(⭐⭐⭐⭐)
    public static void main2(String[] args) throws IOException{
        int n = in.nextInt();
        long[] arrX = new long[n];
        long[] arrY = new long[n];
        long[] arrZ = new long[n];
        long sumX = 0;
        long sumY = 0;
        long sumZ = 0;
        for(int i = 0;i < n;i++){
            int num = in.nextInt();
            arrX[i] += num;
            arrY[i] -= num;
            arrZ[i] -= num;
            sumX += num;sumY -= num;sumZ -= num;
        }
        for(int i = 0;i < n;i++){
            int num = in.nextInt();
            arrX[i] -= num;
            arrY[i] += num;
            arrZ[i] -= num;
            sumX -= num;sumY += num;sumZ -= num;
        }
        for(int i = 0;i < n;i++){
            int num = in.nextInt();
            arrX[i] -= num;
            arrY[i] -= num;
            arrZ[i] += num;
            sumX -= num;sumY -= num;sumZ += num;
        }
        //System.out.println(sumX + " " + sumY + " " + sumZ + " ");
        Arrays.sort(arrX);
        Arrays.sort(arrY);
        Arrays.sort(arrZ);
        //System.out.println(Arrays.toString(arrX));
        //System.out.println(Arrays.toString(arrY));
        //System.out.println(Arrays.toString(arrZ));
        int index = 0;
        // -3 -1  0 1 1 2 4
        // -1 -1  1 2 3 4 5
        // -4 -3 -1 2 2 2 4
        //分别存储对某个阵营不利的事件终止处
        int indexX = n - 1;boolean indexx = false;
        int indexY = n - 1;boolean indeyy = false;
        int indexZ = n - 1;boolean indezz = false;
        for(int i = 0;i < n;i++){
            if(arrX[i] > 0 && !indexx){
                indexX = i - 1;
                indexx = true;
            }
            if(arrY[i] > 0 && !indeyy){
                indexY = i - 1;
                indeyy = true;
            }
            if(arrZ[i] > 0 && !indezz){
                indexZ = i - 1;
                indezz = true;
            }
        }
        //System.out.println("index: " + indexX + " " + indexY + " " + indexZ);
        int sum = 0;
        int sumx = n;int sumy = n;int sumz = n;
        //如果不符合条件,则代表没有有利于该阵营的事件,不可能获胜
        if(indexX >= 0 && indexX < n - 1){
            sumx = n;
            for(int i = 0;i <= indexX && sumX <= 0;i++){
                sumX -= arrX[i];
                //System.out.println(sumX);
                sumx--;
            }
        }
        if(indexY >= 0 && indexY < n - 1){
            sumy = n;
            for(int i = 0;i <= indexY && sumY <= 0;i++){
                sumY -= arrY[i];
                sumy--;
            }
        }
        if(indexZ >= 0 && indexZ < n - 1){
            sumz = n;
            for(int i = 0;i <= indexZ && sumZ <= 0;i++){
                sumZ -= arrZ[i];
                sumz--;
            }
        }
        //System.out.println("sum: " + sumx + " " + sumy + " " + sumz);
        if(sumz == n) sumz = 0;
        if(sumy == n) sumy = 0;
        if(sumx == n) sumx = 0;
        System.out.println(Math.max(Math.max(sumz,sumy),sumx) == 0 ? -1 : Math.max(Math.max(sumz,sumy),sumx));
    }
}
// 11
// 7 17 27 37 47 57 67 77 87 88 98
// 7 17 27 37 57 77 47 67 87 98 88
// 0 0  0  0  0  0  1  1  2  3  4
class Read{
    StringTokenizer st = new StringTokenizer("");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String next() throws IOException{
        while(!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
    long nextLong() throws IOException{
        return Long.parseLong(next());
    }
    double nextDouble() throws IOException{
        return Double.parseDouble(next());
    }
}